package com.jyw.business.obstacles.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.jyw.common.constant.CacheConstants;
import com.jyw.common.core.redis.RedisCache;
import com.jyw.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import com.jyw.business.obstacles.mapper.ObsLocalPhoneMapper;
import com.jyw.business.obstacles.domain.ObsLocalPhone;
import com.jyw.business.obstacles.service.IObsLocalPhoneService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * 报障-座机与用户电话关系Service业务层处理
 * 
 * @author system
 * @date 2023-08-22
 */
@Service
public class ObsLocalPhoneServiceImpl extends ServiceImpl<ObsLocalPhoneMapper,ObsLocalPhone> implements IObsLocalPhoneService
{
    @Resource
    private ObsLocalPhoneMapper obsLocalPhoneMapper;

    @Resource
    private RedisCache redisCache;

    /**
     * 查询报障-座机与用户电话关系
     * 
     * @param id 报障-座机与用户电话关系主键
     * @return 报障-座机与用户电话关系
     */
    @Override
    public ObsLocalPhone selectObsLocalPhoneById(Long id)
    {
        return obsLocalPhoneMapper.selectObsLocalPhoneById(id);
    }

    /**
     * 查询报障-座机与用户电话关系列表
     * 
     * @param obsLocalPhone 报障-座机与用户电话关系
     * @return 报障-座机与用户电话关系
     */
    @Override
    public List<ObsLocalPhone> selectObsLocalPhoneList(ObsLocalPhone obsLocalPhone)
    {
        return obsLocalPhoneMapper.selectObsLocalPhoneList(obsLocalPhone);
    }

    /**
     * 新增报障-座机与用户电话关系
     * 
     * @param obsLocalPhone 报障-座机与用户电话关系
     * @return 结果
     */
    @Override
    public int insertObsLocalPhone(ObsLocalPhone obsLocalPhone)
    {
        obsLocalPhone.setCreateTime(DateUtils.getNowDate());
        return obsLocalPhoneMapper.insertObsLocalPhone(obsLocalPhone);
    }

    /**
     * 修改报障-座机与用户电话关系
     * 
     * @param obsLocalPhone 报障-座机与用户电话关系
     * @return 结果
     */
    @Override
    public int updateObsLocalPhone(ObsLocalPhone obsLocalPhone)
    {
        obsLocalPhone.setUpdateTime(DateUtils.getNowDate());
        return obsLocalPhoneMapper.updateObsLocalPhone(obsLocalPhone);
    }

    /**
     * 批量删除报障-座机与用户电话关系
     * 
     * @param ids 需要删除的报障-座机与用户电话关系主键
     * @return 结果
     */
    @Override
    public int deleteObsLocalPhoneByIds(Long[] ids)
    {
        return obsLocalPhoneMapper.deleteObsLocalPhoneByIds(ids);
    }

    /**
     * 删除报障-座机与用户电话关系信息
     * 
     * @param id 报障-座机与用户电话关系主键
     * @return 结果
     */
    @Override
    public int deleteObsLocalPhoneById(Long id)
    {
        return obsLocalPhoneMapper.deleteObsLocalPhoneById(id);
    }

    @Override
    public Map<String, Long> getSeatCache() {
        if(redisCache.hasKey(CacheConstants.SEAT_NOW)){
            //如果存在直接返回当前缓存
           return redisCache.getCacheMap(CacheConstants.SEAT_NOW);
        }else{
            //如果不存在那么初始化缓存
            List<ObsLocalPhone> list = list();
            Map<String,Long> cache = new HashMap<>();
            for (ObsLocalPhone obsLocalPhone : list) {
                cache.put(obsLocalPhone.getRemark(),null);
            }
            redisCache.setCacheMap(CacheConstants.SEAT_NOW,cache);
            return cache;
        }
    }
}
